package cn.com.sparknet.jsindustrymanager;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import cn.com.sparknet.jsindustrymanager.provider.DBManager;
import cn.com.sparknet.jsindustrymanager.provider.InputDatabaseInvalidException;
import cn.com.sparknet.jsindustrymanager.provider.InputNode;
import cn.emagsoftware.ui.ToastManager;
import cn.emagsoftware.ui.staticmanager.StaticManager;
import cn.emagsoftware.util.LogManager;

public class InputViewActivity extends BaseActivity
{

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        if (!MainActivity.IS_APP_RUNNING)
        {
            StaticManager.restartAppWhenDalvikRecycled(this, MainActivity.class);
            return;
        }
        setContentView(R.layout.inputview);

        String type = getIntent().getStringExtra("TYPE");
        InputNode node = null;
        try
        {
            if ("FLFG".equals(type))
                node = DBManager.queryFLFGInputNode(this, "-1");
            else
                node = DBManager.queryXCSCInputNode(this, "-1");
            if (node == null)
            {
                ToastManager.showLong(this, R.string.inputview_inputdb_empty);
                finish();
                return;
            }
        } catch (InputDatabaseInvalidException e)
        {
            LogManager.logE(InputViewActivity.class, "input database is invalid.", e);
            ToastManager.showLong(this, R.string.inputview_inputdb_invalid);
            finish();
            return;
        }
        FragmentManager fragmentMgr = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentMgr.beginTransaction();
        InputViewFragment inputFragment = new InputViewFragment();
        Bundle inputBundle = new Bundle();
        inputBundle.putString("type", type);
        inputBundle.putSerializable("node", node);
        inputFragment.setArguments(inputBundle);
        transaction.add(R.id.content, inputFragment).commit();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState)
    {
        // TODO Auto-generated method stub
        super.onSaveInstanceState(outState);
        outState.clear();
    }

}
